//
//  CourseCenterInfoView.m
//  xpkc
//
//  Created by TopSageOSX on 14/12/19.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "CourseCenterInfoView.h"

@implementation CourseCenterInfoView{
    
    UIView *courseView;//顶部
    UIView *courseInfoBtnView;
    UIScrollView *courseScroll;//整体
    UIScrollView *courseInfo;//信息
    
    UILabel *courseTitleLabel;
    UILabel *originalPriceLabel;//原价
    UILabel *discountPriceLabel;//折扣价
    UILabel *validTermLabel;//限期
    
    UIButton *introductionBtn;//简介
    UIButton *catalogBtn;//目录
    UIButton *teacherBtn;//咨询
    UIButton *evaluationBtn;//评价
    UIView *courseInfoBtnLine;//按钮下面的线
    //底部视图
    UIView *introductionView;
    UIView *catalogView;
    UIView *teacherView;
    UIView *evaluationView;
    
    UILabel *introductionLabel;
    
    double lastTime;
    float lastOffsetY;
    
    NSMutableArray *sparkArray;
    NSMutableArray *nameArray;
    
    UIView *buyView;
//    UILabel *buyPrice;//购买价
    
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
 
         
        [self creatInfoView];
    }
    return self;
}
- (void)creatInfoView{
    /***************************/
    //创建
    /**
     头部视图，包含图，标题等基本信息和讲师
     */
    courseView = [[UIView alloc]initWithFrame:CGRectMake(50, 0, SCREEN_WIDTH, SCREEN_WIDTH*0.7)];
    
    /**
     覆盖与顶层的滚动视图，装载评价等详细信息
     */
    courseScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-40)];
    courseScroll.delegate = self;
    courseScroll.showsVerticalScrollIndicator = NO;
    courseScroll.showsHorizontalScrollIndicator = NO;
    courseScroll.directionalLockEnabled = YES;
    courseScroll.decelerationRate = 1.0;//基本不减速
    courseScroll.canCancelContentTouches = NO;
    courseScroll.delaysContentTouches = NO;
    courseScroll.tag = 1;
    
    self.courseImg = [[UIImageView alloc]init];
    courseTitleLabel = [[UILabel alloc]init];
    UILabel *priceLabel = [[UILabel alloc]init];
    UIView *priceline = [[UIView alloc]init];//原价删除线
    originalPriceLabel = [[UILabel alloc]init];
    UILabel *discuntLabel = [[UILabel alloc]init];
    discountPriceLabel = [[UILabel alloc]init];
    UILabel *validLabel = [[UILabel alloc]init];
    validTermLabel = [[UILabel alloc]init];
    self.agreementBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    courseInfoBtnView = [[UIView alloc]init];
    courseInfoBtnLine = [[UIView alloc]init];
    
    introductionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    catalogBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    teacherBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    evaluationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    //详细横向滑动的等
    courseInfo = [[UIScrollView alloc]init];
    courseInfo.delegate = self;
    courseInfo.tag = 2;
    
    introductionView = [[UIView alloc]init];
    catalogView = [[UIView alloc]init];
    teacherView = [[UIView alloc]init];
    evaluationView = [[UIView alloc]init];
    
    buyView = [[UIView alloc]init];
    self.addButton=[[UIButton alloc] init];
//    buyPrice = [[UILabel alloc]init];
    self.buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    /***************************/
    //位置
    CGFloat offwidth = 0.04*SCREEN_WIDTH;
    CGFloat imgwidth = 0.64*SCREEN_WIDTH;
    CGRect course_img_frame;
    CGRect title_label_frame;
    CGRect originalPrice_text_frame,originalPrice_lable_frame,originalPrice_line_frame;
    CGRect discountPrice_text_frame,discountPrice_label_frame;
    CGRect validTerm_text_frame,validTerm_label_frame;
    CGRect agreement_btn_frame;
    CGRect courseinfobtn_view_frame,courseinfoline_view_frame;
    CGRect introduction_btn_frame,catalog_btn_frame,teacher_btn_frame,evaluation_btn_frame;
    CGRect introduction_view_frame,catalog_view_frame,teacher_view_frame,evaluation_view_frame;
    CGRect info_scrol_frame;
    CGRect buy_view_frame,buy_label_frame,buy_btn_frame;
    
    course_img_frame = CGRectMake(offwidth, offwidth, imgwidth, imgwidth*9/16);
    title_label_frame = CGRectMake(offwidth, offwidth+imgwidth*9/16, imgwidth, 30);
    originalPrice_text_frame = CGRectMake(offwidth, offwidth+imgwidth*9/16+30, 30, 30);
    originalPrice_lable_frame = CGRectMake(offwidth+30, offwidth+imgwidth*9/16+30, 80, 30);
    originalPrice_line_frame = CGRectMake(offwidth+30, offwidth+imgwidth*9/16+30+15, 45, 2);
    discountPrice_text_frame = CGRectMake(offwidth, offwidth+imgwidth*9/16+30+30, 45, 30);
    discountPrice_label_frame = CGRectMake(offwidth+45, offwidth+imgwidth*9/16+30+30, 80, 30);
    validTerm_text_frame = CGRectMake(offwidth+110, offwidth+imgwidth*9/16+30, 60, 30);
    validTerm_label_frame = CGRectMake(offwidth+110+60, offwidth+imgwidth*9/16+30, 80, 30);
    agreement_btn_frame = CGRectMake(offwidth+140, offwidth+imgwidth*9/16+30+30, 80, 30);
    
    CGFloat leftw = SCREEN_WIDTH/8;
    CGFloat btnw = SCREEN_WIDTH*3/4/4;
    
    courseinfobtn_view_frame = CGRectMake(0, SCREEN_WIDTH*0.7, SCREEN_WIDTH, 30);
    courseinfoline_view_frame = CGRectMake(leftw, 30, btnw, 1);
    introduction_btn_frame = CGRectMake(leftw, 0, btnw, 30);
    catalog_btn_frame = CGRectMake(leftw+btnw, 0, btnw, 30);
    teacher_btn_frame = CGRectMake(leftw+btnw*2, 0, btnw, 30);
    evaluation_btn_frame = CGRectMake(leftw+btnw*3, 0, btnw, 30);
    info_scrol_frame = CGRectMake(0, SCREEN_WIDTH*0.7+30, SCREEN_WIDTH, SCREEN_HEIGHT-64-30-50);
    introduction_view_frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-144);
    catalog_view_frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-144);
    teacher_view_frame = CGRectMake(SCREEN_WIDTH*2, 0, SCREEN_WIDTH, SCREEN_HEIGHT-144);
    evaluation_view_frame = CGRectMake(SCREEN_WIDTH*3, 0, SCREEN_WIDTH, SCREEN_HEIGHT-144);
    
    buy_view_frame = CGRectMake(0, SCREEN_HEIGHT-64-50, SCREEN_WIDTH, 50);
    buy_label_frame = CGRectMake(SCREEN_WIDTH/8, 5, SCREEN_WIDTH*3/4*1/2, 40);
    buy_btn_frame = CGRectMake(SCREEN_WIDTH/8+SCREEN_WIDTH*3/4*1/2, 5, SCREEN_WIDTH*3/4*1/2, 40);
    
    //信息
    self.courseImg.frame = course_img_frame;
    courseTitleLabel.frame = title_label_frame;
    priceLabel.frame = originalPrice_text_frame;
    priceline.frame = originalPrice_line_frame;
    originalPriceLabel.frame = originalPrice_lable_frame;
    discuntLabel.frame = discountPrice_text_frame;
    discountPriceLabel.frame = discountPrice_label_frame;
    validLabel.frame = validTerm_text_frame;
    validTermLabel.frame = validTerm_label_frame;
    self.agreementBtn.frame = agreement_btn_frame;
    
    //其他信息
    courseInfoBtnView.frame = courseinfobtn_view_frame;
    courseInfoBtnLine.frame = courseinfoline_view_frame;
    introductionBtn.frame = introduction_btn_frame;
    catalogBtn.frame = catalog_btn_frame;
    teacherBtn.frame = teacher_btn_frame;
    evaluationBtn.frame = evaluation_btn_frame;
    courseInfo.frame = info_scrol_frame;
    introductionView.frame = introduction_view_frame;
    catalogView.frame = catalog_view_frame;
    teacherView.frame = teacher_view_frame;
    evaluationView.frame = evaluation_view_frame;
    
    //购买
    buyView.frame = buy_view_frame;
    self.addButton.frame=buy_label_frame;
//    buyPrice.frame = buy_label_frame;
    self.buyBtn.frame = buy_btn_frame;
    
    /***************************/
    //
    //详情
    self.courseImg.image = [UIImage imageNamed:@"abc"];
    courseView.backgroundColor = [UIColor whiteColor];
    courseTitleLabel.font = FONT_BOLD(17);
    courseTitleLabel.text = @"2014年保过班，包过啊";
    
    priceLabel.text = @"原价:";
    priceLabel.font = FONT_SYS_XS;
    
    priceline.backgroundColor = [UIColor lightGrayColor];
    
    originalPriceLabel.text = @"￥1000";
    originalPriceLabel.font = FONT_SYS_XS;
    originalPriceLabel.textColor = [UIColor lightGrayColor];
    
    discuntLabel.text = @"优惠价:";
    discuntLabel.font = FONT_SYS_XS;
    
    discountPriceLabel.text = @"￥999";
    discountPriceLabel.font = [UIFont systemFontOfSize:17];
    discountPriceLabel.textColor = [UIColor redColor];
    
    validLabel.text  = @"有效期至:";
    validLabel.font = FONT_SYS_XS;
    
    validTermLabel.text = @"2015-11-11";
    validTermLabel.font = FONT_SYS_XS;
    
    [self.agreementBtn setTitle:@"查看课程协议" forState:UIControlStateNormal];
    self.agreementBtn.titleLabel.font = FONT_SYS_XS;
    self.agreementBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.agreementBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    courseScroll.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+SCREEN_WIDTH*0.7-34);
//    courseScroll.backgroundColor = [UIColor redColor];
    
   
    
    courseInfoBtnView.backgroundColor = [UIColor colorWithRed:0.99 green:0.99 blue:0.99 alpha:1];
    UIView *courseline = [[UIView alloc]initWithFrame:CGRectMake(0, courseInfoBtnView.height-1, SCREEN_WIDTH, 1)];
    courseline.tag = 789;
    courseline.backgroundColor = [UIColor lightGrayColor];
    [courseInfoBtnView addSubview:courseline];
    courseInfoBtnLine.backgroundColor = [UIColor colorWithRed:0/255 green:140.0/255 blue:70.0/255 alpha:1];
    courseInfoBtnLine.tag = 10;
    
    
    [introductionBtn setTitle:@"简介" forState:UIControlStateNormal];
    [introductionBtn setTitleColor:[UIColor colorWithRed:0/255 green:140.0/255 blue:70.0/255 alpha:1] forState:UIControlStateSelected];
    [introductionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    introductionBtn.tag = 0;
    [introductionBtn addTarget:self action:@selector(infoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    introductionBtn.titleLabel.font = FONT_SYS_S;
    //默认选中
    introductionBtn.selected = YES;
    introductionBtn.transform = CGAffineTransformMakeScale(1.1, 1.1);
    
    [catalogBtn setTitle:@"目录" forState:UIControlStateNormal];
    [catalogBtn setTitleColor:[UIColor colorWithRed:0/255 green:140.0/255 blue:70.0/255 alpha:1] forState:UIControlStateSelected];
    [catalogBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    catalogBtn.tag = 1;
    [catalogBtn addTarget:self action:@selector(infoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    catalogBtn.titleLabel.font = FONT_SYS_S;
    
    [teacherBtn setTitle:@"讲师" forState:UIControlStateNormal];
    [teacherBtn setTitleColor:[UIColor colorWithRed:0/255 green:140.0/255 blue:70.0/255 alpha:1] forState:UIControlStateSelected];
    [teacherBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    teacherBtn.tag = 2;
    [teacherBtn addTarget:self action:@selector(infoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    teacherBtn.titleLabel.font = FONT_SYS_S;
    
    [evaluationBtn setTitle:@"评价" forState:UIControlStateNormal];
    [evaluationBtn setTitleColor:[UIColor colorWithRed:0/255 green:140.0/255 blue:70.0/255 alpha:1] forState:UIControlStateSelected];
    [evaluationBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    evaluationBtn.tag = 3;
    [evaluationBtn addTarget:self action:@selector(infoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    evaluationBtn.titleLabel.font = FONT_SYS_S;
    
    courseInfo.contentSize = CGSizeMake(SCREEN_WIDTH*4, SCREEN_HEIGHT-64-30-50);
    courseInfo.pagingEnabled = YES;
    
    introductionView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    [self creatIntroductionView];
    catalogView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    [self creatCatalogView];
    teacherView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    evaluationView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    
    buyView.layer.borderWidth = 1;
    buyView.backgroundColor = [UIColor whiteColor];
    
    self.addButton.backgroundColor=OrangeColor;
    [self.addButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.addButton setTitle:@"加入购物车" forState:(UIControlStateNormal)];
//    addButton.cornerRadios=6;
//    buyPrice.backgroundColor = OrangeColor;
//    buyPrice.textColor = [UIColor whiteColor];
//    buyPrice.textAlignment = NSTextAlignmentCenter;
//    buyPrice.text = @"￥:999";
//    buyPrice.cornerRadiosLeft = 6;
    
    [self.buyBtn setBackgroundColor:[UIColor colorWithRed:0/255 green:140.0/255 blue:70.0/255 alpha:1]];
    [self.buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    self.buyBtn.titleLabel.font = FONT_BOLD(16);
//    self.buyBtn.cornerRadiosRight = 6;
    
    /***************************/
    //添加
    [courseView addSubview:self.courseImg];
    [courseView addSubview:courseTitleLabel];
    [courseView addSubview:priceLabel];
    [courseView addSubview:originalPriceLabel];
    [courseView addSubview:priceline];
    [courseView addSubview:discuntLabel];
    [courseView addSubview:discountPriceLabel];
    [courseView addSubview:validLabel];
    [courseView addSubview:validTermLabel];
    [courseView addSubview:self.agreementBtn];
    [self addSubview:courseView];
    [courseInfoBtnView addSubview:introductionBtn];
    [courseInfoBtnView addSubview:catalogBtn];
    [courseInfoBtnView addSubview:teacherBtn];
    [courseInfoBtnView addSubview:evaluationBtn];
    [courseInfoBtnView addSubview:courseInfoBtnLine];
    [courseInfo addSubview:introductionView];
    [courseInfo addSubview:catalogView];
    [courseInfo addSubview:teacherView];
    [courseInfo addSubview:evaluationView];
    [courseScroll addSubview:courseInfo];
    [courseScroll addSubview:courseInfoBtnView];//顺序
    [self addSubview:courseScroll];
    [buyView addSubview:self.addButton];
//    [buyView addSubview:buyPrice];
    
    [buyView addSubview:self.buyBtn];
    [self addSubview:buyView];
    [self setValueForInfoView];
    
    [self bringSubviewToFront:courseView];
    
}

/**
 *  简介页
 */
- (void)creatIntroductionView{
    //
    introductionLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.04, SCREEN_WIDTH*0.04, SCREEN_WIDTH*0.9, SCREEN_HEIGHT-64-30-50)];
    introductionLabel.numberOfLines = 0;
    [introductionView addSubview:introductionLabel];
}
/**
 *  目录页
 */
- (void)creatCatalogView{
}
/**
 *  赋值
 */
- (void)setValueForInfoView{
    /*****************
     *  顶部的
     */
    courseTitleLabel.text = self.courseTitle;
    originalPriceLabel.text = [NSString stringWithFormat:@"￥%@",self.originalPrice];
    discountPriceLabel.text = [NSString stringWithFormat:@"￥%@",self.discountPrice];
    
    
    if (self.validTerm==NULL||self.validTerm.length==0) {
        validTermLabel.text=@"已过期";
    }else{
      validTermLabel.text=self.validTerm;
    }
//    double time = [self.validTerm doubleValue];
//    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
//    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//    [formatter setDateFormat:@"yyyy-MM-dd"];
//    validTermLabel.text = [formatter stringFromDate:date];
//    if (time <= 1) {
//        validTermLabel.text = @"已过期";
//    }
    
    /******************/
    //底部的
    //简介
    NSAttributedString * introduction = [[NSAttributedString alloc] initWithData:[self.courseIntroduction dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    CGRect rect = [introduction boundingRectWithSize:introductionLabel.frame.size options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    introductionLabel.height = rect.size.height;
    [introductionLabel setAttributedText:introduction];
    
    //目录
//    logdebug(@"%@",self.catalogDic);
    sparkArray = [NSMutableArray array];
    nameArray = [NSMutableArray array];
    //:目录的位置安排
    CGFloat offwith       = 0.04*SCREEN_WIDTH;
    CGFloat totalHeight   = 0;
    CGFloat classHeight   = 0.10*SCREEN_WIDTH;
    CGFloat classWight    = SCREEN_WIDTH-2*offwith;
    CGFloat chapterHeight = 0.08*SCREEN_WIDTH;
    CGFloat chapterWight  = SCREEN_WIDTH-3*offwith;
    CGFloat sectionHeight = 0.06*SCREEN_WIDTH;
    CGFloat sectionWight  = SCREEN_WIDTH-4*offwith;
    
    UIScrollView *catalogscroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-40-50)];
    catalogscroll.delegate = self;
    catalogscroll.tag = 20;
    [catalogView addSubview:catalogscroll];
//    logdebug(@"%@",self.catalogArray);
    //FIXME:配色方案待确定
    for (int i = 0; i<self.catalogArray.count; i++) {
        //班次信息
        NSDictionary *catalogDic = [self.catalogArray objectAtIndex:i];
        
        NSString *className = [catalogDic objectForKey:@"lectureName"];

        UIButton *classBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        classBtn.titleLabel.font = FONT_SYS_NORMAL;
        [classBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [classBtn setTitle:className forState:UIControlStateNormal];
        classBtn.userInteractionEnabled = NO;
//        classBtn.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
        [classBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [classBtn setFrame:CGRectMake(offwith, totalHeight, classWight, classHeight)];
        totalHeight += classHeight;
        [catalogscroll addSubview:classBtn];
        
        NSArray *lecturearray =
        [catalogDic objectForKey:@"lectures"];
        NSMutableArray *chapterArray = [NSMutableArray array];
        for (NSDictionary *dic in lecturearray) {
            NSString *chapter = [dic objectForKey:@"lecture_chapter"];
            BOOL had = NO;
            for (int j = 0; j<chapterArray.count; j++) {
                if ([chapter isEqualToString:[chapterArray objectAtIndex:j]]) {
                    had = YES;
                }
            }
            BOOL ispub = [[dic objectForKey:@"ispub"]boolValue];
            if (had) {
                //添加节
                UIButton *sectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                [sectionBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
                sectionBtn.titleLabel.font = FONT_SYS_S;
                
                if (ispub) {
                    sectionBtn.userInteractionEnabled = YES;
                    [sectionBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                    [sectionBtn setTitle:[NSString stringWithFormat:@"%@(点击试听)",[dic objectForKey:@"class_name"]] forState:UIControlStateNormal];
                    [sparkArray addObject:[dic objectForKey:@"spark"]];
                    [nameArray addObject:[NSString stringWithFormat:@"%@  %@",[dic objectForKey:@"lecture_chapter"],[dic objectForKey:@"class_name"]]];
                    [sectionBtn addTarget:self action:@selector(listenTest:) forControlEvents:UIControlEventTouchUpInside];
                    sectionBtn.tag = sparkArray.count*100;
                }else{
                    [sectionBtn setTitle:[dic objectForKey:@"class_name"] forState:UIControlStateNormal];
                    sectionBtn.userInteractionEnabled = NO;
                    [sectionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                }
//                sectionBtn.backgroundColor = [UIColor colorWithRed:0.99 green:0.99 blue:0.99 alpha:1];
                [sectionBtn setFrame:CGRectMake(offwith*3, totalHeight, sectionWight, sectionHeight)];
                totalHeight += sectionHeight;
                
                [catalogscroll addSubview:sectionBtn];
                
            }else{
                //添加新章
                UIButton *chapterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                [chapterBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
                chapterBtn.titleLabel.font = FONT_SYS_S;
                [chapterBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
                [chapterBtn setFrame:CGRectMake(offwith*2, totalHeight, chapterWight, chapterHeight)];
                totalHeight += chapterHeight;
//                chapterBtn.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
                [chapterBtn setTitle:chapter forState:UIControlStateNormal];
                
                [catalogscroll addSubview:chapterBtn];
                
                [chapterArray addObject:chapter];
                //添加节
                UIButton *sectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                [sectionBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
                sectionBtn.titleLabel.font = FONT_SYS_S;
                
                if (ispub) {
                    [sectionBtn setTitle:[NSString stringWithFormat:@"%@(点击试听)",[dic objectForKey:@"class_name"]] forState:UIControlStateNormal];
                    [sectionBtn addTarget:self action:@selector(listenTest:) forControlEvents:UIControlEventTouchUpInside];
                    sectionBtn.userInteractionEnabled = YES;
                    [sectionBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                    [sparkArray addObject:[dic objectForKey:@"spark"]];
                    [nameArray addObject:[NSString stringWithFormat:@"%@  %@",[dic objectForKey:@"lecture_chapter"],[dic objectForKey:@"class_name"]]];
                    sectionBtn.tag = sparkArray.count*100;
                }else{
                    [sectionBtn setTitle:[dic objectForKey:@"class_name"] forState:UIControlStateNormal];
                    sectionBtn.userInteractionEnabled = NO;
                    [sectionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                }
//                sectionBtn.backgroundColor = [UIColor colorWithRed:0.99 green:0.99 blue:0.99 alpha:1];
                [sectionBtn setFrame:CGRectMake(offwith*3, totalHeight, sectionWight, sectionHeight)];
                totalHeight += sectionHeight;
                
                [catalogscroll addSubview:sectionBtn];
            }
        }
    }
    catalogscroll.contentSize = CGSizeMake(SCREEN_WIDTH, totalHeight);
    
    //讲师
    for (int i = 0; i<self.teacherArray.count; i++) {
        NSDictionary*dic = [self.teacherArray objectAtIndex:i];
        UIImageView *teacherheader = [[UIImageView alloc]initWithFrame:CGRectMake(offwith*2, offwith*2, 40, 40)];
        [teacherheader setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.studypay.com%@",[dic objectForKey:@"litpic"]]] placeholderImage:[UIImage imageNamed:@""]];
        logdebug(@"%@",[dic valueForKey:@"experience"]);
        logdebug(@"%@",[dic valueForKey:@"feature"]);
        //FIXME:讲师头像圆角
        teacherheader.cornerRadios = 20;
        teacherheader.layer.masksToBounds = YES;
        [teacherView addSubview:teacherheader];
        
        UILabel *teacherLabel = [[UILabel alloc]initWithFrame:CGRectMake(offwith*2, offwith*2+40, 40, 20)];
        teacherLabel.font = FONT_SYS_XS;
        teacherLabel.textAlignment = NSTextAlignmentCenter;
        teacherLabel.text = [dic objectForKey:@"title"];
        teacherLabel.textColor = OrangeColor;
        [teacherView addSubview:teacherLabel];
        
        UILabel *featureLabel = [[UILabel alloc]initWithFrame:CGRectMake(offwith*3 + 40, offwith*2, SCREEN_WIDTH - offwith*4 - 40, 40)];
        featureLabel.text = [dic valueForKey:@"feature"];
        featureLabel.font = FONT_SYS_XS;
        featureLabel.numberOfLines = 0;
        [teacherView addSubview:featureLabel];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(offwith, offwith*4 + 39, SCREEN_WIDTH - offwith*2, 0.5)];
        line.backgroundColor = [UIColor lightGrayColor];
        [teacherView addSubview:line];
    }
    //评价
    //FIXME:评价
    logdebug(@"%@",
    self.evaluationArray);
    if (self.evaluationArray.count>0) {
        NSDictionary *dic = [self.evaluationArray objectAtIndex:0];
        NSString *answer = [dic objectForKey:@"answer"];
        NSString *ask = [dic objectForKey:@"ask"];
        logdebug(@"%@\n%@",ask,answer);
        
        UIImageView *askImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
        askImg.layer.cornerRadius = 20;
        askImg.clipsToBounds = YES;
        [askImg setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.studypay.com%@",[dic objectForKey:@"face"]]]];
        [evaluationView addSubview:askImg];
        
        CGSize asksize = [ask sizeWithFont:FONT_SYS_NORMAL constrainedToSize:CGSizeMake(SCREEN_WIDTH-60, 1000) lineBreakMode:NSLineBreakByCharWrapping];
        UILabel *asklabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, SCREEN_WIDTH-60, asksize.height)];
        asklabel.numberOfLines = 0;
        asklabel.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
        asklabel.text = ask;
        asklabel.font = FONT_SYS_NORMAL;
        asklabel.layer.cornerRadius = 4;
        asklabel.layer.borderWidth = 2;
        asklabel.layer.borderColor = [UIColor whiteColor].CGColor;
        asklabel.clipsToBounds = YES;
        [evaluationView addSubview:asklabel];
        
        CGSize answersize = [answer sizeWithFont:FONT_SYS_NORMAL constrainedToSize:CGSizeMake(SCREEN_WIDTH-60, 1000) lineBreakMode:NSLineBreakByWordWrapping];
        if (asksize.height<50) {
            asksize.height = 50;
        }
        UILabel *answerlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, asksize.height+15, SCREEN_WIDTH-60, answersize.height)];
        answerlabel.text = answer;
        answerlabel.font = FONT_SYS_NORMAL;
        answerlabel.numberOfLines = 0;
        answerlabel.clipsToBounds = YES;
        answerlabel.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
        answerlabel.layer.cornerRadius = 4;
        answerlabel.layer.borderWidth = 2;
        answerlabel.layoutMargins = UIEdgeInsetsMake(3, 3, 3, 3);
        answerlabel.layer.borderColor = [UIColor whiteColor].CGColor;
        [evaluationView addSubview:answerlabel];
        UIImageView *answerimg = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-50, asksize.height+15, 40, 40)];
        answerimg.clipsToBounds = YES;
        answerimg.layer.cornerRadius = 20;
        answerimg.image = [UIImage imageNamed:@"icon"];
        [evaluationView addSubview:answerimg];
    }
    
    //
//    buyPrice.text = self.discountPrice;
}
/**
 *  重绘图片大小
 *
 *  @param image  图
 *  @param reSize 大小
 *
 *  @return 图
 */
- (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return reSizeImage;
    
}
- (void)listenTest:(UIButton *)btn{
    [self.delegate listenTestWithSpark:[sparkArray objectAtIndex:btn.tag/100-1] andVideoName:[nameArray objectAtIndex:btn.tag/100-1]];
}
#pragma mark -
#pragma mark == func ==
/**
 *  详情按钮点击
 *
 *  @param btn ann
 */
- (void)infoBtnClick:(UIButton *)btn{
    for (UIView *view in courseInfoBtnView.subviews) {
        if (view.tag<5) {
            //复原按钮
            UIButton *btn = (UIButton *)view;
            btn.selected = NO;
            btn.transform = CGAffineTransformMakeScale(1, 1);
        }
    }
    [UIView animateWithDuration:0.3 animations:^{
        //
        btn.selected = YES;
        btn.transform = CGAffineTransformMakeScale(1.1, 1.1);
        courseInfoBtnLine.frame = CGRectMake(SCREEN_WIDTH/8+SCREEN_WIDTH*3/4/4*btn.tag, 30, SCREEN_WIDTH*3/4/4, 2);
        courseInfo.contentOffset = CGPointMake(SCREEN_WIDTH*btn.tag, 0);
    }];
}

#pragma mark -
#pragma mark == scrollview delegate ==

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.tag == 1) {
        
        if (scrollView.contentOffset.y >= 0.7 *SCREEN_WIDTH) {
            //定高到导航栏
            courseInfoBtnView.frame = CGRectMake(0, scrollView.contentOffset.y, SCREEN_WIDTH, 30);
        }else{
            //底部的时候
            courseInfoBtnView.frame = CGRectMake(0, 0.7 *SCREEN_WIDTH, SCREEN_WIDTH, 30);
        }
        courseView.frame = CGRectMake(50, -scrollView.contentOffset.y/3, SCREEN_WIDTH, SCREEN_WIDTH*0.7);
        if (scrollView.contentOffset.y >= 1) {
            [self bringSubviewToFront:courseScroll];
            [self bringSubviewToFront:buyView];
            [scrollView bringSubviewToFront:courseInfoBtnView];
        }else{
            [self bringSubviewToFront:courseView];
        }
    }
    
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //目录列表的滚动
    if (scrollView.tag == 20) {
        if (scrollView.contentOffset.y <= -20) {
            //拉下来
            [UIView animateWithDuration:1 animations:^{
                courseScroll.contentOffset = CGPointMake(courseScroll.contentOffset.x, 1);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.1 animations:^{
                    courseScroll.contentOffset = CGPointMake(courseScroll.contentOffset.x, 3);
                } completion:^(BOOL finished) {
                    [UIView animateWithDuration:0.1 animations:^{
                        courseScroll.contentOffset = CGPointMake(courseScroll.contentOffset.x, 0);
                    }];
                }];
            }];
            
        }else if (scrollView.contentOffset.y >= 1){
            [UIView animateWithDuration:1 animations:^{
                courseScroll.contentOffset = CGPointMake(courseScroll.contentOffset.x, 0.7*SCREEN_WIDTH+2);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.1 animations:^{
                    courseScroll.contentOffset = CGPointMake(courseScroll.contentOffset.x, 0.7*SCREEN_WIDTH);
                }];
            }];
        }
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.tag == 2) {
        //
        for (UIView *view in courseInfoBtnView.subviews) {
            if (view.tag<5) {
                //复原按钮
                UIButton *btn = (UIButton *)view;
                btn.selected = NO;
                btn.transform = CGAffineTransformMakeScale(1, 1);
                if (btn.tag == (int)(scrollView.contentOffset.x/SCREEN_WIDTH)) {
                    [UIView animateWithDuration:0.3 animations:^{
                        //
                        btn.selected = YES;
                        btn.transform = CGAffineTransformMakeScale(1.1, 1.1);
                        courseInfoBtnLine.frame = CGRectMake(SCREEN_WIDTH/8+SCREEN_WIDTH*3/4/4*btn.tag, 30, SCREEN_WIDTH*3/4/4, 2);
                        courseInfo.contentOffset = CGPointMake(SCREEN_WIDTH*btn.tag, 0);
                    }];
                }
            }
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
